package com.oss.service.oss;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.oss.common.BaseResponse;
import com.oss.dto.oss.purchasecourses.*;
import com.oss.dto.oss.student.StudentAddDto;
import com.oss.dto.oss.student.StudentGetByIDDto;
import com.oss.dto.oss.student.StudentPageDto;
import com.oss.dto.oss.student.StudentUpdateDto;
import com.oss.entity.oss.PurchaseCourses;
import com.oss.entity.oss.Student;
import com.oss.enums.IsDeletedEnum;
import com.oss.mapper.oss.PurchaseCoursesMapper;
import com.oss.mapper.oss.StudentMapper;
import com.oss.service.system.DictionaryService;
import com.oss.service.system.UserService;
import com.oss.util.DownloadUtil;
import com.oss.util.SnowFlakeUtil;
import com.oss.util.TemplatePathUtil;
import com.oss.util.easyexcel.EasyExcelExportByTemplateUtil;
import com.oss.vo.oss.purchasecourses.PurchaseCoursesVO;
import com.oss.vo.oss.student.StudentExportVO;
import com.oss.vo.oss.student.StudentVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.FileInputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author chengqiang
 */
@Service
public class PurchaseCoursesService extends ServiceImpl<PurchaseCoursesMapper, PurchaseCourses> {
    @Autowired
    DictionaryService dictionaryService;
    @Autowired
    UserService userService;
    @Autowired
    StudentService studentService;

    public IPage<PurchaseCoursesVO> selectByPage(PurchaseCoursesPageDto dto) {
        LambdaQueryWrapper<PurchaseCourses> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(PurchaseCourses::getIsDeleted, IsDeletedEnum.NO.getFlag());
        queryWrapper.eq(StringUtils.isNotBlank(dto.getStudentID()),PurchaseCourses::getStudentID, dto.getStudentID());
        queryWrapper.eq(StringUtils.isNotBlank(dto.getType()),PurchaseCourses::getType, dto.getType());
        //queryWrapper.like(dto.getVipServer() != null,PurchaseCourses::getVipServer, dto.getVipServer());
        queryWrapper.eq(dto.getPurchaseDate() != null,PurchaseCourses::getPurchaseDate, dto.getPurchaseDate());
        IPage<PurchaseCourses> ipage = this.page(new Page<>(dto.getPageNo(), dto.getPageSize()), queryWrapper);
        Map<String,String> userMap = new HashMap<>();
        Map<String,String> dictionaryMap = new HashMap<>();
        Map<String,StudentVO> studentMap = new HashMap<>();
        if (CollectionUtils.isNotEmpty(ipage.getRecords())) {
            // 获取创建人和修改人ID
            Set<String> ids = ipage.getRecords().stream().map(PurchaseCourses::getCreatorId).collect(Collectors.toSet());
            ids.addAll(ipage.getRecords().stream().map(PurchaseCourses::getUpdatorId).collect(Collectors.toSet()));
            userMap = userService.selectByIds(ids);
            // 获取自媒体平台字典值
            dictionaryMap = dictionaryService.getDictionaryMapByTypeCode("PurchaseCoursesType");
            Set<String> studentIDs = ipage.getRecords().stream().map(PurchaseCourses::getStudentID).collect(Collectors.toSet());
            studentMap = studentService.selectByIDs(studentIDs);
        }
        Map<String, String> finalUserMap = userMap;
        Map<String, String> finalDictionaryMap = dictionaryMap;
        Map<String, StudentVO> finalStudentMap = studentMap;
        return ipage.convert(entity->{
            PurchaseCoursesVO vo = new PurchaseCoursesVO();
            BeanUtils.copyProperties(entity,vo);
            vo.setCreatorName(finalUserMap.get(entity.getCreatorId()));
            vo.setUpdatorName(finalUserMap.get(entity.getUpdatorId()));
            vo.setType(finalDictionaryMap.get(entity.getType()));
            if (finalStudentMap.containsKey(entity.getStudentID())){
                StudentVO studentVO = finalStudentMap.get(entity.getStudentID());
                vo.setStudentID(studentVO.getId());
                vo.setStudentPhone(studentVO.getPhone());
                vo.setStudentName(studentVO.getName());
                vo.setStudentWechatID(studentVO.getWechatID());
                vo.setStudentAccount(studentVO.getAccount());
                vo.setStudentWeMediaPlatform(studentVO.getWeMediaPlatform());
                vo.setStudentPlatformNickname(studentVO.getPlatformNickname());
                vo.setStudentRecommendedDate(studentVO.getRecommendedDate());
                vo.setStudentWechatNickname(studentVO.getWechatNickname());
                vo.setCode(studentVO.getCode());
            }
            return  vo;
        });
    }

    public String add(PurchaseCoursesAddDto dto) {
        PurchaseCourses entity = new PurchaseCourses();
        BeanUtils.copyProperties(dto, entity);
        entity.setId(SnowFlakeUtil.getID());
        this.save(entity);
        return "操作成功";
    }

    public String updateData(PurchaseCoursesUpdateDto dto) {
        PurchaseCourses entity = new PurchaseCourses();
        BeanUtils.copyProperties(dto, entity);
        this.updateById(entity);
        return "操作成功";
    }

    public String delete(PurchaseCoursesDeleteDto dto) {
        LambdaUpdateWrapper<PurchaseCourses> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.eq(PurchaseCourses::getId, dto.getId());
        lambdaUpdateWrapper.set(PurchaseCourses::getIsDeleted, IsDeletedEnum.YES.getFlag());
        this.update(null,lambdaUpdateWrapper);
        return "操作成功";
    }
}